

$(document).keyup(function (event) {
    switch (event.keyCode) {
        case 13: {
            queryBtn_Filter();//重新查询数据
            /*var isHidden_win = $("#win").parent().is(":hidden");
            var isHidden_win_common = $("#win_common").parent().is(":hidden");

            if(isHidden_win == true && isHidden_win_common==true){
               queryBtn_Filter();//重新查询数据
               break;
            }
            //提交form
            if(isHidden_win == false){
                formAddSubmit();
                break;
            }*/
            break;
        }
        case 27:
        case 96: {
            $('#win').window('close');
            $('#win_common').window('close');
            break;
        }
    }
});

var heightRealGlobal = 0;
$(function () {

    keyRandom = $('#keyRandom').val();
    //close the window
    $('#win').dialog('close');
    $('#win_common').dialog('close');

    //添加数据
    $('#form_addButton').click(function () {
        formAddSubmit();
    });

    //双击datagrid,弹编辑框
    datagrid.datagridObj.onDblClickRow = function (rowIndex, rowData) {
        editRow(rowIndex);
    };

    //============resize condition layout===========
    function resizeConditionLayoutHeight() {
        //condition div content height
        var heightReal = $("#_conditionContentDiv").height();
        if (heightReal != 0 && heightReal != heightRealGlobal) {
            heightRealGlobal = heightReal;
            var height = heightReal + 15;
            $('body').layout('panel', 'north').panel('resize', {height: height + "px"});
            $('body').layout('resize');
        }
    }

    //没有查询条件的话，删除掉north区
    var heightReal = $("#_conditionContentDiv").height();
    if (heightReal == 0) {
        $('body').layout('remove', 'north');
    } else {
        resizeConditionLayoutHeight();
        setInterval(resizeConditionLayoutHeight, 500);// 注意函数名没有引号和括弧！
    }

    window.onload = inputPlaceholder();

});


function downloadReport() {
    //document.downloadReportForm.submit();
    var url = 'template_downloadReport.action?_keyRandom=' + _keyRandom;
    var exportExcel = "export_excel";
    //dataParams[exportExcel] = 1;
    //var params = $.param(dataParams);
    //var url = host+"&"+params;
    $('<form method="post" action="' + url + '"></form>').appendTo('body').submit().remove();
    //delete dataParams[exportExcel];
};

/*tool bar button on click listen*/
function toolbarBtnOnClick(url, isDialog, width, height, itemName) {
    //上个页面通过右击列表 传过来的 id和row; ps:右击模板x列，看模板列列表，自动加载列数，要传模板id找表名
    //var prePageListRow = '${paramValues.row[0]}';
    //prePageListRow = decodeURI(prePageListRow);
    var _id = _prePageListId;
    /*
    if(_prePageListRow!=''){
    	_id = jQuery.parseJSON(_prePageListRow).id;
    }
    */

    //============
    var rows = $('#' + datagrid.datagridId).datagrid('getSelections');

    //row 中含有json子对象或双引号，传给下一页parse会出错 ，所以这里就不传了
    var rowIdsArr = new Array();
    $.each(rows, function (index, row) {
        var rowIds = {'id': row.id};
        rowIdsArr.push(rowIds);
    });

    //
    var rowIdsArr = JSON.stringify(rowIdsArr);

    rowIdsArr = encodeURI(rowIdsArr);
    rowIdsArr = encodeURI(rowIdsArr);

    if (url.indexOf('?') != -1) {
        url = url + '&rowIdsArr=' + rowIdsArr + '&_id=' + _id;
    } else {
        url = url + '?rowIdsArr=' + rowIdsArr + '&_id=' + _id;
    }

    //
    if (isDialog == '1') {
        //url = _path+'/'+url+'?_id='+_id;
        url = _path + '/' + url;
        //居中显示
        var iframeWidth = $(this).width();
        var iframeHeight = $(this).height();
        var setWidth = (iframeWidth - width) / 2;
        var setHeight = (iframeHeight - height) / 2;

        $.iframe.win({
            url: url,
            title: itemName,
            width: width,
            height: height,
            modal: false,
            left: setWidth,
            top: setHeight
        });

    } else {
        //上个页面通过右击列表 传过来的 id和row; ps:右击模板x列，看模板列列表，自动加载列数，要传模板id找表名
        //url = url +'?_id='+_id;
        window.parent.Open(itemName, url);
    }
};

//判断obj是否为json对象
function isJson(obj) {
    var isjson = typeof (obj) == "object" && Object.prototype.toString.call(obj).toLowerCase() == "[object object]" && !obj.length;
    return isjson;
}

/*
right menu button on click listen
href：只会引入body部分内容
*/
function rightMenuItemClick(url, isDialog, width, height, itemName) {
    var row = $('#' + datagrid.datagridId).datagrid('getData').rows[rowIndex];
    //row 中含有json子对象或双引号，传给下一页parse会出错 ，所以这里就不传了
    //row.chartJson='';
    $.each(row, function (key, value) {
        try {
            if (value != '' && isNaN(value)) {
                if (isJson(value)) {
                    row[key] = '';
                } else if (value.indexOf("'") != -1) {
                    row[key] = '';
                } else if (value.indexOf("\"") != -1) {
                    row[key] = '';
                } else if (value.indexOf("\{") != -1) {
                    row[key] = '';
                }
            }
        } catch (er) {
            window.parent.error(er);
        }

    })

    //
    var rowtext = JSON.stringify(row);
    rowtext = encodeURI(rowtext);
    rowtext = encodeURI(rowtext);

    if (url.indexOf('?') != -1) {
        url = url + '&row=' + rowtext + '&_id=' + row.id;
        //url = url+'&row=999&_id='+row.id;
    } else {
        url = url + '?row=' + rowtext + '&_id=' + row.id;
        //url = url+'?row=888&_id='+row.id;
    }
    if (isDialog == '1') {
        url = _path + '/' + url;
        //居中显示
        var iframeWidth = $(this).width();
        var iframeHeight = $(this).height();
        var setWidth = (iframeWidth - width) / 2;
        var setHeight = (iframeHeight - height) / 2;

        $.iframe.win({
            url: url,
            title: itemName,
            width: width,
            height: height,
            modal: true,
            left: setWidth,
            top: setHeight
        });


    } else {
        //alert(url)
        //window.parent.Open(itemName+id,url+'?row='+row);
        window.parent.Open(itemName + row.id, url);
    }
}

function rightMenuItemClickV2(url, isDialog, width, height, itemName, submitText, callback) {

    var tab_title = window.parent.getSelectedTabTitle();
    var jsonParam = {};
    jsonParam.isDialog = isDialog;
    jsonParam.url = _path + url;
    jsonParam.title = tab_title + "-" + itemName + "-" + rowIndex;
    jsonParam.width = width;
    jsonParam.height = height;
    //jsonParam.dialog_style = _style4AddDialog;
    jsonParam.buttons = [];
    jsonParam.buttons[jsonParam.buttons.length] = {
        text: submitText,
        iconCls: 'icon-save',
        handler: callback
    }
    jsonParam.buttons[jsonParam.buttons.length] = {
        text: '关闭',
        iconCls: 'icon-cancel',
        handler: function () {
            $.iframe.close();
        }
    }

    var row = $('#' + datagrid.datagridId).datagrid('getData').rows[rowIndex];
    row = JSON.stringify(row);
    jsonParam.parameter1_iframedialog = row;
    jsonParam.parameter2_iframedialog = "344";
    jsonParam.parameter3_iframedialog = "565";
    openUrl(jsonParam);
}


function lineTailBtnOnClick(url, isDialog, width, height, itemName, rowIndex) {
    var row = $('#' + datagrid.datagridId).datagrid('getData').rows[rowIndex];
    //row 中含有json子对象或双引号，传给下一页parse会出错 ，所以这里就不传了
    //row.chartJson='';
    $.each(row, function (key, value) {
        try {
            if (value != '' && isNaN(value)) {
                if (value.indexOf("'") != -1) {
                    row[key] = '';
                } else if (value.indexOf("\"") != -1) {
                    row[key] = '';
                } else if (value.indexOf("\{") != -1) {
                    row[key] = '';
                }
            }
        } catch (er) {
            window.parent.error(er);
        }

    })

    var rowtext = JSON.stringify(row);
    rowtext = encodeURI(rowtext);
    rowtext = encodeURI(rowtext);

    if (url.indexOf('?') != -1) {
        url = url + '&row=' + rowtext + '&_id=' + row.id;
    } else {
        url = url + '?row=' + rowtext + '&_id=' + row.id;
    }
    if (isDialog == '1') {
        url = _path + '/' + url;
        //居中显示
        var iframeWidth = $(this).width();
        var iframeHeight = $(this).height();
        var setWidth = (iframeWidth - width) / 2;
        var setHeight = (iframeHeight - height) / 2;
        $.iframe.win({
            url: url,
            title: itemName,
            width: width,
            height: height,
            modal: false,
            left: setWidth,
            top: setHeight
        });

    } else {
        window.parent.Open(itemName + row.id, url);
    }
}

function removeRow(rowIndex) {
    $.messager.confirm('Confirm', '确定删除这条记录吗？', function (r) {
        if (r) {
            var row = $('#' + datagrid.datagridId).datagrid('getData').rows[rowIndex];
            var id = row[_primaryKey];
            var paramData = {};
            paramData[_primaryKey] = id;
            //remove ajax
            $.ajax({
                type: "POST",
                url: _path + _formRemoveAction,
                dataType: "json",
                data: paramData,
                success: function (msg) {
                    if (JSON.stringify(msg).indexOf('error:') == 0) {
                        window.parent.error(JSON.stringify(msg));
                    } else {
                        $.messager.alert("操作结果", msg[0].responseContent);
                        //成功
                        if (msg[0].responseCode == 1) {
                            datagrid_fresh();
                        }
                    }
                },
                error: function (er) {
                    window.parent.error(er);
                }
            });
        }
    });
}


function toolbar_add() {
    var tab_title = window.parent.getSelectedTabTitle();
    var jsonParam = {};
    jsonParam.isDialog = _isdialog4AddDialog;
    jsonParam.url = _path + _formAddEditUrl;
    jsonParam.title = tab_title + "-添加框";
    jsonParam.width = 800;
    jsonParam.height = 400;
    jsonParam.buttons = [];
    jsonParam.buttons[jsonParam.buttons.length] = {
        text: '保存',
        iconCls: 'icon-save',
        handler: function () {
            formAddSubmit();
        }
    }
    jsonParam.buttons[jsonParam.buttons.length] = {
        text: '关闭',
        iconCls: 'icon-cancel',
        handler: function () {
            $.iframe.close();
        }
    }
    jsonParam.dialog_style = _style4AddDialog;

    //自动选中上页传过来的id, 作为外键
    var _foreignKeyJo = {};
    _foreignKeyJo[_foreignKey] = _prePageListId;
    var paramJson = JSON.stringify(_foreignKeyJo);
    /*paramJson = decodeURI(paramJson);
    paramJson = decodeURI(paramJson);
    paramJson = decodeURI(paramJson);*/

    jsonParam.parameter1_iframedialog = paramJson;
    jsonParam.parameter2_iframedialog = "23";
    jsonParam.parameter3_iframedialog = "56";
    openUrl(jsonParam);
}

//修改数据
function editRow(rowIndex) {
    var requestUrl = _formAddAction;
    authRequestUrl(requestUrl, function (authResult) {
        editRowInner(rowIndex, authResult);
    });
}

/**
 * @param rowIndex
 * @param authResult: when true then hidden save button
 */
function editRowInner(rowIndex, authResult) {
    var tab_title = window.parent.getSelectedTabTitle();
    var jsonParam = {};
    jsonParam.isDialog = _isdialog4AddDialog;
    jsonParam.url = _path + _formAddEditUrl;
    jsonParam.title = tab_title + "-详细-" + rowIndex;
    jsonParam.width = 800;
    jsonParam.height = 400;
    jsonParam.dialog_style = _style4AddDialog;
    jsonParam.buttons = [];
    //是否有权限访问
    if (authResult) {
        jsonParam.buttons[jsonParam.buttons.length] = {
            text: '保存',
            iconCls: 'icon-save',
            handler: function () {
                formAddSubmit();
            }
        }
    }
    jsonParam.buttons[jsonParam.buttons.length] = {
        text: '关闭',
        iconCls: 'icon-cancel',
        handler: function () {
            $.iframe.close();
        }
    }

    var row = $('#' + datagrid.datagridId).datagrid('getData').rows[rowIndex];
    row = JSON.stringify(row);
    jsonParam.parameter1_iframedialog = row;
    jsonParam.parameter2_iframedialog = "344";
    jsonParam.parameter3_iframedialog = "565";
    openUrl(jsonParam);
}

//生成任务
function assignTask(rowIndex) {
    var requestUrl = _formAssignTaskAction;
    authRequestUrl(requestUrl, function (authResult) {
        assignTaskInner(rowIndex, authResult);
    });
}

/**
 * @param rowIndex
 * @param authResult: when true then hidden save button
 */
function assignTaskInner(rowIndex, authResult) {
    var tab_title = window.parent.getSelectedTabTitle();
    var jsonParam = {};
    jsonParam.isDialog = _isdialog4AddDialog;
    jsonParam.url = _path + _formAddEditUrl_1;
    jsonParam.title = tab_title + "-指派任务-" + rowIndex;
    jsonParam.width = 800;
    jsonParam.height = 400;
    jsonParam.dialog_style = _style4AddDialog;
    jsonParam.buttons = [];
    //是否有权限访问
    if (authResult) {
        jsonParam.buttons[jsonParam.buttons.length] = {
            text: '生成任务',
            iconCls: 'icon-save',
            handler: function () {
                // alert(sss);
                window.frames['iframe_dialog'].formAddSubmitJustAdd();
                // formAddSubmit();
            }
        }
    }
    jsonParam.buttons[jsonParam.buttons.length] = {
        text: '关闭',
        iconCls: 'icon-cancel',
        handler: function () {
            $.iframe.close();
        }
    }

    var row = $('#' + datagrid.datagridId).datagrid('getData').rows[rowIndex];
    row = JSON.stringify(row);
    jsonParam.parameter1_iframedialog = row;
    jsonParam.parameter2_iframedialog = "344";
    jsonParam.parameter3_iframedialog = "565";
    openUrl(jsonParam);
}


/**
 * 调用iframe中的formAddSubmit()
 * 增加form表单提交
 */
function formAddSubmit() {
    window.frames['iframe_dialog'].formAddSubmit();
}

function closeWinCommon() {
    $.iframe.close();
}


